<template>
  <div class="main">
    <img src="../assets/image/login.png" alt="" class="leftImg" />
    <div class="rightBox">
      <div class="title">登录</div>
      <div class="label">账号</div>
      <el-input v-model="formData.username" placeholder="用户名/账号" :prefix-icon="User" />
      <div class="label">密码</div>
      <div>
        <el-input
          type="password"
          v-model="formData.psd"
          show-password
          placeholder="密码"
          :prefix-icon="Lock"
        />
      </div>
      <div class="label">验证码</div>
      <div v-if="captchaOnOff" style="display: flex; align-items: center">
        <el-input
          v-model="formData.code"
          @keyup.enter="submitForm"
          auto-complete="off"
          placeholder="请输入验证码"
        >
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </div>

      <el-button class="btn" type="primary" @click="submitForm"> 登录 </el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { getCodeImg, login, getRouters, getInfo } from '@/api/login'
import { User, Lock } from '@element-plus/icons-vue'
import { goPage } from '@/utils/commonFn'
const formData = ref({
  username: '',
  psd: '',
  password: '',
  code: '',
  uuid: ''
})
const codeUrl = ref('')
// 验证码开关
const captchaOnOff = ref(true)
onMounted(() => {
  getCode()
})
// 获取验证码
const getCode = () => {
  getCodeImg().then((res: any) => {
    captchaOnOff.value = res.captchaOnOff === undefined ? true : res.captchaOnOff
    if (captchaOnOff.value) {
      codeUrl.value = 'data:image/gif;base64,' + res.img
      formData.value.uuid = res.uuid
    }
  })
}
/**
 * 加密算法
 */
const encodePass = (oldPassword: string) => {
  oldPassword += '&tq&' + new Date().getTime()

  var passwordKey = '987612435zxcvbnmopasdfuighjkqwertyl!@#$%^*()-+='
  var len = passwordKey.length
  var a = passwordKey.split('')
  var s = '',
    b,
    b1,
    b2,
    b3
  for (var i = 0; i < oldPassword.length; i++) {
    b = oldPassword.charCodeAt(i)
    b1 = b % len
    b = (b - b1) / len
    b2 = b % len
    b = (b - b2) / len
    b3 = b % len
    s += a[b3] + a[b2] + a[b1]
  }

  return s
}
const submitForm = async () => {
  let errText = ''
  if (!formData.value.username) {
    errText = '请输入用户名/账号'
  } else if (!formData.value.psd) {
    errText = '请输入密码'
  } else if (!formData.value.code) {
    errText = '请输入验证码'
  }
  if (errText) {
    ElMessage({
      message: errText,
      type: 'error'
    })
    return
  }
  try {
    formData.value.password = encodePass(formData.value.psd)
    //@ts-ignore
    delete formData.value.psd
    const res = await login(formData.value)
    sessionStorage.setItem('xunfei_token', res?.data.access_token)
    // sessionStorage.setItem('dept_name', res?.data.dept_name)
    const res1: any = await getInfo()
    sessionStorage.setItem('userPermissions', JSON.stringify(res1.permissions))
    sessionStorage.setItem('userInfo', JSON.stringify(res1.user))

    // const res2 = await getRouters()
    // sessionStorage.setItem('routers', JSON.stringify(res2.data))
    ElMessage({
      message: '登录成功',
      type: 'success'
    })
    goPage({
      path: '/',
      menuName: '工作台'
    })
    setTimeout(() => {
      window.location.reload()
    }, 50)
  } catch (error) {
    if (captchaOnOff.value) {
      getCode()
    }
  }
}
</script>

<style scoped lang="less">
.main {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  display: flex;
  background-color: #fff;
}
.leftImg {
  width: 50%;
  height: 100%;
}
.rightBox {
  width: 50%;
  height: 100%;
  padding: 205px 200px;
  .title {
    font-size: 28px;
    font-weight: 600;
    color: #171721;
    margin-bottom: 61px;
  }
  .label {
    font-size: 18px;
    color: #171721;
    line-height: 25px;
    margin: 10px 0;
  }

  :deep(.el-input__wrapper) {
    background: #f5f9fe !important;
    height: 48px;
    border-radius: 8px;
  }
  .btn {
    width: 100%;
    height: 48px;
    background: #3e7eff;
    border-radius: 8px;
    margin-top: 60px;
  }
}
.login-code {
  width: 33%;
  height: 48px;
  margin-left: 10px;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
</style>
